package com.lagou.edu.message.mq;

import com.alibaba.fastjson.JSON;
import com.lagou.edu.common.constant.MQConstant;
import com.lagou.edu.common.mq.dto.BaseMqDTO;
import com.lagou.edu.common.mq.listener.AbstractMqListener;
import com.lagou.edu.course.api.CourseRemoteService;
import com.lagou.edu.course.api.dto.CourseDTO;
import com.lagou.edu.message.api.dto.Message;
import com.lagou.edu.message.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zlg
 * @create 2021-02-08 0:00
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = MQConstant.Topic.COURSE_RELEASE_SEND_MESSAGE, consumerGroup = "${rocketmq.producer.group}" + "_" + MQConstant.Topic.COURSE_RELEASE_SEND_MESSAGE)
public class CourseReleaseSendMessageListener extends AbstractMqListener<BaseMqDTO<Integer>> implements RocketMQListener<BaseMqDTO<Integer>> {
    @Autowired
    private IMessageService messageService;
    @Autowired
    private CourseRemoteService courseRemoteService;
    
    @Override
    public void onMessage(BaseMqDTO<Integer> data) {
        log.info("onMessage -data: {}", JSON.toJSONString(data));
        if (this.checkMessageId(data.getMessageId())) {
            return;
        }
        final Integer courseId = data.getData();
        /*if (CollectionUtils.isEmpty(userIdList)) return;
        Message message = null;
        for (final Integer userid : userIdList) {
            message = new Message();
            message.setUserId(userid);
            message.setContent();
            messageService.sendMessage(message);
        }*/
        final CourseDTO courseDTO = courseRemoteService.getCourseById(courseId);
        final Message message = new Message();
        message.setContent(courseDTO.getCourseName() + "课程新上线，欢迎试学");
        messageService.sendMessage(message);
        this.updateMessageId(data.getMessageId());
        
    }
}
